package com.qiantu.phone.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.BaseAdapter;
import com.hjq.widget.layout.SettingBar;
import com.lxj.xpopup.XPopup;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.entity.ControlDataAirConditionerBean;
import com.qiantu.api.entity.ControlDataFloorHeatingBean;
import com.qiantu.api.entity.ControlDataFreshAirBean;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.DeviceModeValue;
import com.qiantu.api.entity.LinkageBean;
import com.qiantu.api.entity.LinkageNewBean;
import com.qiantu.api.utils.DeviceInfoParseUtil;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.ui.adapter.DeviceModeAdapter;
import com.qiantu.phone.ui.adapter.ListLabelAdapter;
import com.qiantu.phone.ui.dialog.BottomListDialog;
import com.qiantu.phone.ui.uiutil.DeviceMainType;
import com.qiantu.phone.ui.uiutil.LLDeviceUtil;
import com.qiantu.phone.utils.LinkageDataHolder;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * time   : 2021/12/22
 */
public final class SelectDeviceModeActivityNew extends AppActivity {
    private SettingBar settingAction;
    private TextView tvCondition;
    private RecyclerView recyclerView;
    private DeviceModeAdapter mDeviceModeAdapter;
    private String deviceName;
    private LinkageNewBean mLinkageBean;
    private LinkageNewBean.LinkageDeviceCondition mLinkageCondition;
    private BottomListDialog mBottomActionDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_select_device_mode_new;
    }

    @Override
    protected void initView() {
        mLinkageCondition =  getSerializable("condition");
        String type = getString("type");
        String deviceType = getString("device_type");
        String actionType = getString("action_type");
        List<DeviceModeValue> deviceModeValues = new ArrayList<>();
        if(DeviceMainType.AIRCONDITIONER.stringType.equals(type)){
            ControlDataAirConditionerBean airConditionerInfo = DeviceInfoParseUtil.parseData(getContext(), deviceType, ControlDataAirConditionerBean.class);
            if(actionType.equals(LLDeviceUtil.MODE)){
                setTitle(getString(R.string.Linkage_Device_Mode));
                deviceModeValues = airConditionerInfo.getControlData().getMode();
            }else if(actionType.equals(LLDeviceUtil.WIND_SPEED)){
                setTitle(getString(R.string.Linkage_Device_WindSpeed));
                deviceModeValues = airConditionerInfo.getControlData().getWindspeed();
            }else if(actionType.equals(LLDeviceUtil.WIND_DIRECTION)){
                setTitle(getString(R.string.Linkage_Device_SwingWind));
                deviceModeValues = airConditionerInfo.getControlData().getWinddirection();
            }
        }else if(DeviceMainType.FRESHAIR.stringType.equals(type)){
            ControlDataFreshAirBean freshAirInfo = DeviceInfoParseUtil.parseData(getContext(), deviceType, ControlDataFreshAirBean.class);
            if(actionType.equals(LLDeviceUtil.MODE)){
                setTitle(getString(R.string.Linkage_Device_Mode));
                deviceModeValues = freshAirInfo.getControlData().getMode();
            }else if(actionType.equals(LLDeviceUtil.WIND_SPEED)){
                setTitle(getString(R.string.Linkage_Device_WindSpeed));
                deviceModeValues = freshAirInfo.getControlData().getWindspeed();
            }else if(actionType.equals(LLDeviceUtil.EXHAUST_SPEED)){
                setTitle(getString(R.string.Linkage_Device_ExhaustwindSpeed));
                deviceModeValues = freshAirInfo.getControlData().getExhaustspeed();
            }
        }else if(DeviceMainType.FLOORHEATING.stringType.equals(type)){
            setTitle(getString(R.string.Linkage_Device_Mode));
            ControlDataFloorHeatingBean floorHeatingInfo = DeviceInfoParseUtil.parseData(getContext(), deviceType, ControlDataFloorHeatingBean.class);
            deviceModeValues = floorHeatingInfo.getControlData().getMode();
        }
        settingAction = findViewById(R.id.setting_action);
        tvCondition = findViewById(R.id.tv_condition);
        recyclerView = findViewById(R.id.recycler_view);
        setOnClickListener(settingAction);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mDeviceModeAdapter = new DeviceModeAdapter(getContext());
        recyclerView.setAdapter(mDeviceModeAdapter);
        mDeviceModeAdapter.setData(deviceModeValues);
        mDeviceModeAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
                DeviceModeValue deviceModeValue = mDeviceModeAdapter.getData().get(position);
                mLinkageCondition.setBeginValue(deviceModeValue.getValue());
                int res = getResources().getIdentifier(deviceModeValue.getName(), "string", getContext().getPackageName());
                tvCondition.setText(getString(R.string.effective_when_time_holder, getString(res)));
            }
        });
    }

    @Override
    protected void initData() {
        mLinkageBean = LinkageDataHolder.getLinkageDataNew();

        //当前联动空，才去
        if (mLinkageCondition == null) {
            deviceName = getString("device_name");
            mLinkageCondition = new LinkageNewBean.LinkageDeviceCondition();
            mLinkageCondition.setConditionTypeId(8);
            mLinkageCondition.setAreaTypeId(1);
            mLinkageCondition.setCompareTypeId(2);
            mLinkageCondition.setActionTypeId(1);

            mLinkageCondition.setDeviceSerialNo(getString("device_serial_no"));
            mLinkageCondition.setAreaName(getString("floor_room_name"));
            mLinkageCondition.setAreaCode(getString("room_serial_no"));
            mLinkageCondition.setBeginValue(mLinkageCondition.getBeginValue());
            mLinkageCondition.setEndValue(mLinkageCondition.getBeginValue());
            mLinkageCondition.setIcon(getString("device_image"));
            mLinkageCondition.setDescription(deviceName);
            mLinkageCondition.setStateName(LLDeviceUtil.MODE);
            mLinkageCondition.setSort(mLinkageBean.getConditions().indexOf(mLinkageCondition) + 1);
        }else{
            DeviceBean deviceBean = LLDeviceDBManager.getInstance(getContext()).getDeviceBySerialNo(mLinkageCondition.getDeviceSerialNo());
            if(deviceBean != null){
                deviceName = deviceBean.getName();
            }
        }
        updateView();
    }

    private void updateView() {
        DeviceModeValue deviceModeValue = mDeviceModeAdapter.checkSelected((int)mLinkageCondition.getBeginValue());
        if(deviceModeValue != null){
            int res = getResources().getIdentifier(deviceModeValue.getName(), "string", getContext().getPackageName());
            tvCondition.setText(getString(R.string.effective_when_time_holder, getString(res)));
        }

        settingAction.setRightText(mLinkageCondition.getActionTypeId() == 1 ? R.string.linkage_action_become : R.string.linkage_action_keep);
    }

    @Override
    public void onRightClick(View view) {
        if(mDeviceModeAdapter.getSelected() == null){
            toast(R.string.please_select_specific_mode);
            return;
        }
        if(getBoolean("isAdd")){
            mLinkageBean.getConditions().add(mLinkageCondition);
        }else{
            int position = getInt("position");
            mLinkageBean.getConditions().remove(position);
            mLinkageBean.getConditions().add(position,mLinkageCondition);
        }

        startActivity_SingleTask(EditLinkageActivityNew.class);
        finish();
    }
    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.setting_action:
                showBottomListDialog();
                break;
        }
    }

    private void showBottomListDialog() {
        if (mBottomActionDialog == null) {
            mBottomActionDialog = new BottomListDialog(this);
            mBottomActionDialog.setDialogClickListener(new BottomListDialog.DialogClickListener() {
                @Override
                public void onConfirmClick() {

                }
            });
            ListLabelAdapter listLabelAdapter = new ListLabelAdapter(this);
            List<String> data = new ArrayList<>();
            data.add("变为");
            data.add("处于");
            listLabelAdapter.setData(data);
            listLabelAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
                    mLinkageCondition.setActionTypeId(position + 1);
                    settingAction.setRightText(mLinkageCondition.getActionTypeId() == 1 ? R.string.linkage_action_become : R.string.linkage_action_keep);
                    mBottomActionDialog.dismiss();
                }
            });
            mBottomActionDialog.setAdapter(listLabelAdapter);
            new XPopup.Builder(this)
                    .dismissOnBackPressed(true)
                    .enableDrag(true)
                    .asCustom(mBottomActionDialog);
        }
        ListLabelAdapter listLabelAdapter = (ListLabelAdapter) mBottomActionDialog.getAdapter();
        listLabelAdapter.setSelected("");
        mBottomActionDialog.show();
    }
}